package com.space.controller;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.space.base.ApiResponse;
import com.space.base.ErrorCode;
import com.space.common.HashUtil;
import com.space.config.ConfigUtil;
import com.space.config.ConfigurationJdbc;
import com.space.config.DB;
import com.space.config.Tables;
import com.space.entity.UiConfiguration;
import com.space.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/user")
@Slf4j
@Api("登陆接口")
public class UserController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final UserRowMapper userRowMapper = new UserRowMapper();
    private static class UserRowMapper implements RowMapper<User> {
        @Override
        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User();
            user.setId(rs.getString("id"));
            user.setPassword(rs.getString("password"));
            return user;
        }
    }

    @PostConstruct
    public void init() {

        //插入默认用户名admin
//        try{
            String sql = "INSERT INTO ui_user (password, id) VALUES (?, ?)";
            String id="admin";
            String password=HashUtil.hashPassword("123456");



            if(DB.db1.data.equals(Tables.activeDB)) {
                jdbcTemplate.update(Tables.getCreateUserSql(DB.db1.data));

                List<User> vs = jdbcTemplate.query("SELECT * FROM ui_user ", new Object[]{}, userRowMapper);
                if(vs.isEmpty()) jdbcTemplate.update(sql, password, id);
            }
            if(DB.db2.data.equals(Tables.activeDB)) {
                jdbcTemplate.update(Tables.getCreateUserSql(DB.db2.data));

                List<User> vs = jdbcTemplate.query("SELECT * FROM ui_user ", new Object[]{}, userRowMapper);
                if(vs.isEmpty()) jdbcTemplate.update(sql, password, id);

            }
            if(DB.db3.data.equals(Tables.activeDB)) {
                jdbcTemplate.update(Tables.getCreateUserSql(DB.db3.data));

                List<User> vs = jdbcTemplate.query("SELECT * FROM ui_user ", new Object[]{}, userRowMapper);
                if(vs.isEmpty()) jdbcTemplate.update(sql, password, id);
            }
            if(DB.db4.data.equals(Tables.activeDB)) {
                jdbcTemplate.update(Tables.getCreateUserSql(DB.db4.data));

                List<User> vs = jdbcTemplate.query("SELECT * FROM ui_user ", new Object[]{}, userRowMapper);
                if(vs.isEmpty()) jdbcTemplate.update(sql, password, id);
            }
            if(DB.db5.data.equals(Tables.activeDB)) {
                jdbcTemplate.update(Tables.getCreateUserSql(DB.db5.data));

                List<User> vs = jdbcTemplate.query("SELECT * FROM ui_user ", new Object[]{}, userRowMapper);
                if(vs.isEmpty()) jdbcTemplate.update(sql, password, id);
            }
//        }catch (Exception e){}

    }


    @PostMapping("/login")
    @ApiOperation("login")
    public ApiResponse<String> login(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);
            String sql = "SELECT * FROM ui_user WHERE id = ?";
            List<User> vs = jdbcTemplate.query(sql, new Object[]{obj.getString("id")}, userRowMapper);
            boolean lable =false;
            if(!vs.isEmpty()){
                String dbpass = vs.get(0).password;
                lable =HashUtil.verifyPassword(obj.getString("password"),dbpass);
            }

            if(lable){
                ApiResponse<String> result = new ApiResponse();
                return result;
            }else{
                ApiResponse<String> result = new ApiResponse();
                result.setReCode(ErrorCode.User_Passwod_CODE.intValue());
                result.setReMsg(ErrorCode.User_Passwod__MSG);

                return result;
            }

        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }



}


